package edu.bu.cs673.superkids123.children.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.AbstractView;
import edu.bu.cs673.superkids123.children.DataObjectAccess.LessonGameManager;
import edu.bu.cs673.superkids123.children.controller.ActivitiesMenuController;
import edu.bu.cs673.superkids123.children.model.Game;
import edu.bu.cs673.superkids123.children.model.Lesson;
import edu.bu.cs673.superkids123.children.model.Theme;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.common.controller.exceptions.PlayedTooLongException;
import edu.bu.cs673.superkids123.common.view.LoginView;

/**
 * A menu screen that allows the child to start a lesson or play a game, select his/her avatar for
 * customization, or go back. Parents may login from this screen as well.
 */
public class ActivitiesMenuView extends AbstractNavigationView implements ActionListener {

	private static int H_GAP = 20;
	private static int V_GAP = H_GAP;

	private static int ACTIVITY_COLS = 3;

	private JPanel panel = new JPanel(new GridLayout(0, ACTIVITY_COLS, H_GAP, V_GAP));
	private ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance(); 
	private JButton[] activities;

	public ActivitiesMenuView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	@Override
	protected JPanel layout() {

		// Header
		// [back] Welcome, kid! [change avatar]
		setAvatarVisible(true);
		setTitleBorderVisible(true);
		setParentsButtonVisible(true);

		// Activities selection area
		// TODO
		if (activities == null) {
			Lesson[] lessons = LessonGameManager.getLessons();
			Game[] games = LessonGameManager.getGames();
			activities = new JButton[lessons.length + games.length];
	
			for (int i = 0; i < lessons.length; i++) {
				activities[i] = new JButton(lessons[i].getName());
				
				if (lessons[i].getIcon() != null)
					activities[i].setIcon(provider.getImage(lessons[i].getIcon()));
				
				activities[i].setFont(Theme.CHILD_FONT);
				activities[i].setContentAreaFilled(false);
				activities[i].addActionListener(this);
				panel.add(activities[i]);
			}
	
			for (int i = lessons.length; i < (lessons.length + games.length); i++) {
				activities[i] = new JButton( games[(i - lessons.length)].getName());
				
				if (games[(i - lessons.length)].getIcon() != null)
					activities[i].setIcon(provider.getImage(games[(i - lessons.length)].getIcon()));
				
				activities[i].setFont(Theme.CHILD_FONT);
				activities[i].setContentAreaFilled(false);
				activities[i].addActionListener(this);
				panel.add(activities[i]);
			}
		}

		// Footer
		setParentsButtonVisible(true);

		return panel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();

		for (int i = 0; (i < activities.length); i++) {
			if (source.equals(activities[i])) {
				getMainFrame().getController(ActivitiesMenuController.class).launch(
						e.getActionCommand());
				break;
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see edu.bu.cs673.superkids123.AbstractNavigationView#initializeView()
	 */
	protected AbstractView<? extends JComponent> initializeView() {
		super.initializeView();

		
		try {
			authController.checkPlayTime();
		} catch (PlayedTooLongException e) {
			authController.logout();
			JOptionPane.showMessageDialog(null,
					 "You have played too much today! Please talk to you parents about playing more.",
					 "Played too long!", JOptionPane.OK_OPTION);
			return getMainFrame().getView(LoginView.class);
		}
		
		// Fall back is to return this view
		return this;
	}

	@Override
	public String getTitle() {
		return ("Welcome back, " + authController.getCurrentUser().getName() + "!");
	}

	@Override
	public String getBorderTitle() {
		return (" Activities ");
	}
}